package com.endurance.dao.impl;

import com.endurance.dao.AccountDao;
import com.endurance.mapper.TransferMapper;
import com.endurance.pojo.Transfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private TransferMapper transferMapper;

    /**
     * 重置账户余额
     *
     * @param transfer
     */
    public void reset(Transfer transfer) {
        String sql = "update transfer SET account = ? where name = ?";
        jdbcTemplate.update(sql, transfer.getAccount(), transfer.getName());
    }

    /**
     * 修改账户信息
     *
     * @param transfer
     */
    public void update(Transfer transfer) {
        String sql = "update transfer SET account = account + ? where name = ?";
        jdbcTemplate.update(sql, transfer.getAccount(), transfer.getName());
    }

    public List<Transfer> list() {
        return jdbcTemplate.query("select * from transfer", transferMapper);
    }
}
